﻿#pragma once


#include <QDialog>
#include <QPushButton>
#include "QListView"
#include "QStringListModel"
#include "QFileDialog"
#include "QMessageBox"
#include "QApplication"
#include "QVBoxLayout"
#include <QDesktopWidget>
#include <QLibrary>
#include <QApplication>
#include <QLabel>
#include <QComboBox>
#include "QListView"
#include "QStringListModel"
#include "QLineEdit"
#include "QSpinBox"
#include "QRadioButton"
#include <QSettings>

#include "commonfunction.h"

#include "halcon_opencv.h"

using namespace std;

#include "qt_graphics_view.h"

#include <QSettings>

#include "show_qlabel.h"


class Function:public QDialog
{
public:
    QSettings* m_qsettings;
   

    //1 0k;0 ng; -1 empty;

    vector<int> m_vector_result;

    QString m_model_image_path = "model.png";

    QLabel * m_qlabel_threshold_hole;
    QSpinBox * m_qspinbox_threshold_hole;
    QSlider* m_qslider_threshold_hole;

    QLabel* m_qlabel_times;
	QLabel* m_qspinbox_times;
    int m_times=1;

	QLabel* m_qlabel_number_rows;
	int m_number_rows = 1;

    QLabel* m_qlabel_ball_number;
    int m_ball_number=0;


	

    QLabel* m_qlabel_match_radius;
    QSpinBox* m_qspinbox_match_radius;
    QSlider*  m_qslider_match_radius;

	QLabel* m_qlabel_ball_threshold;
	QSpinBox* m_qspinbox_ball_threshold;
	QSlider* m_qslider_ball_threshold;

	QLabel* m_qlabel_inner_radius;
	QSpinBox* m_qspinbox_inner_radius;

    QLabel* m_qlabel_center_threshold;
    QSpinBox* m_qspinbox_center_threshold;

    QLabel* m_qlabel_center_size;
    QSpinBox* m_qspinbox_center_size;


	QLabel* m_qlabel_outer_radius;
	QSpinBox* m_qspinbox_outer_radius;

    QLabel* m_qlabel_error_gray;
    QSpinBox* m_qspinbox_error_gray;
    QSlider* m_qslider_error_gray;

    QLabel* m_qlabel_error_size;
    QSpinBox* m_qspinbox_error_size;
    QSlider* m_qslider_error_size;


    Mat m_image ;
    QString m_path_para = "para.json";
    QString m_section   = "";

    QtQGraphicsView* m_view ;

    ShowQLabel* m_showqlabel=nullptr;

    QPushButton* m_pushbutton_load_image;
    QPushButton* m_pushbutton_load_dir;
    QListView* m_listview;
    QStringListModel* m_listview_model;



    Mat image_out;
    QComboBox* m_qcombobox_unuse_use;
    int m_unuse_use_function = 0;

    QComboBox* m_qcombobox_save_wrong;
    int m_save_wrong = 0;

    QComboBox* m_qcombobox_save_right;
    int m_save_right = 0;


    QPushButton* m_pushbutton_save_load_image;

	QPushButton* m_pushbutton_show_orign_image;
    QPushButton* m_pushbutton_detection;
    QPushButton* m_pushbutton_save_parameter;
    QPushButton* m_pushbutton_return;

    Mat m_image_show;

    int m_result;
    double m_match_radius_now;

    HObject m_regionhole,m_regionball;
    HObject m_regioninner,m_error_region , m_regioncenter  , m_regionouter;

public:
    explicit Function(QDialog* parent, QString path_in, QString section);



    bool detection(HObject image_in, QString& message);



    void detectionImage();

    void draw(Mat image_in, Mat& image_out);

    void listViewClick(const QModelIndex& model_index);

    void loadParameter(QString path_in, QString section);

    void saveParameter();

    void createShowImage(int value_ball_number, int value_ball_row, vector<int > m_vector_result, Mat& image_out)
    {
        int width = 225;
        int height = 50;
        image_out = Mat(height, width, CV_8UC3, Scalar(255, 255, 255));



        for (int i = 0; i < value_ball_row; i++)
        {
            for (int j = 0; j < value_ball_number; j++)
            {
                Scalar scalar;
                if (m_vector_result[i*value_ball_number + j] == -1)
                {
                    scalar = Scalar(0, 0, 255);
                }
                if (m_vector_result[i*value_ball_number + j] == -2)
                {
                    scalar = Scalar(0, 153, 255);
                }
                if (m_vector_result[i*value_ball_number + j] == 1)
                {
                    scalar = Scalar(0, 255, 0);
                }
                if (m_vector_result[i*value_ball_number + j] == 0)
                {
                    scalar = Scalar(0, 0, 0);
                }
                cv::circle(image_out, Point(j * 30 + 5, i * 20 + 15), 5, scalar, -1);
            }
        }
    }
};
#pragma execution_character_set("utf-8")
